package service

import (
	"github.com/crack007/go-im/src/common/constant"
	"github.com/crack007/go-im/src/common/model"
	"github.com/crack007/go-im/src/common/util"
	"strconv"
)

type userService struct {
}

func (s *userService) UserOnline(guestModel *model.UserModel) {
	util.GetRedis().HSet(constant.OnlineUser, strconv.Itoa(int(guestModel.Id)), guestModel.NodeName)
}

func (s *userService) UserOffline(guestModel *model.UserModel) {
	util.GetRedis().HDel(constant.OnlineUser, strconv.Itoa(int(guestModel.Id)))
}

func (s *userService) GetUser(id uint32) *model.NodeModel {
	nodeModel := &model.NodeModel{}
	nodeModel.Id, _ = util.GetRedis().HGet(constant.OnlineUser, strconv.Itoa(int(id))).Result()
	node := NodeService.Get(nodeModel.Id)
	if node == nil {
		return nil
	}
	nodeModel.Port = node.Port
	nodeModel.WsPort = node.WsPort
	nodeModel.Ip = node.Ip
	return nodeModel
}
